/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/07/31
 */
package vn.smartware.co.server.action;

import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.framework.action.BaseAction;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;
import vn.smartware.co.framework.form.CommonSearchForm;
import vn.smartware.co.server.dao.factory.AbstractCommonSearchDAO;
import vn.smartware.co.server.dao.factory.DaoFactory;

/**
 * CommonSearchAction
 * @author thuctt
 */
@SuppressWarnings({"rawtypes"})
public class CommonSearchAction extends BaseAction {

	private static Log log = LogFactory.getLog(CommonSearchAction.class);
	
	/**
	 * get drink list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getDrinkList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		CommonSearchForm form = (CommonSearchForm)aForm;
		AbstractCommonSearchDAO dao = DaoFactory.createCommonSearchDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList list = dao.getDrinkList(form, user, conn);
						
			form.setList(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	/**
	 * get food list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getFoodList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
	throws ServerException {
		
		CommonSearchForm form = (CommonSearchForm)aForm;
		AbstractCommonSearchDAO dao = DaoFactory.createCommonSearchDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList list = dao.getFoodList(form, user, conn);
			
			form.setList(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	/**
	 * get room list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getRoomList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
	throws ServerException {
		
		CommonSearchForm form = (CommonSearchForm)aForm;
		AbstractCommonSearchDAO dao = DaoFactory.createCommonSearchDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList list = dao.getRoomList(form, user, conn);
			
			form.setList(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get customer list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getCustomerList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		CommonSearchForm form = (CommonSearchForm) aForm;
		AbstractCommonSearchDAO dao = DaoFactory.createCommonSearchDAO();
		Connection conn = getConnection();

		try {

			LoginDTO user = (LoginDTO) form.getUserDTO();
			ArrayList list = dao.getCustomerList(form, user, conn);

			form.setList(list);

		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
}
